package com.fancon.android.gardenjokesx.activity;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListView;
import android.widget.TextView;

import com.fancon.android.gardenjokesx.GardenJokesBaseActivity;
import com.fancon.android.gardenjokesx.R;
import com.fancon.android.gardenjokesx.adapter.FriendsAdapter;
import com.fancon.android.gardenjokesx.entity.User;
import com.fancon.android.gardenjokesx.rpc.GardenJokesApiConnect;
import com.fancon.android.gardenjokesx.rpc.JsonPaser;
import com.fancon.android.gardenjokesx.utils.Constant;
import com.fancon.android.gardenjokesx.utils.UserUtil;
import com.fancon.android.multithread.RequestUI;
import com.fancon.android.ui.widget.FanconProgressBar;

public class S05Activity extends GardenJokesBaseActivity{

	private ArrayList<User> mListUser = new ArrayList<User>();
	private ListView mList;
	private String userID;
	private int mPageIndex = 1;
	private FriendsAdapter mAdapter;
	private Boolean isFirst = true;
	private boolean loadingMore = false;
	private Boolean isEnd = false;
	private View footerView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.s05);
		User user = UserUtil.getInfoUserLogin(getApplicationContext());
		if(user != null){
			userID = user.getUser_id();
		}
		//@binhbt-comment
		TextView title = (TextView) findViewById(R.id.action_bar_textview_title);
		title.setText(getText(R.string.S05_title));
		LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View backBtn = findViewById(R.id.action_bar_button_back);
		backBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		footerView = layoutInflater.inflate(R.layout.listfooter, null, false);
		
		mList = (ListView) findViewById(R.id.s05_listUserRecommend);
		mList.setCacheColorHint(0);
		mList.setDivider(null);
		mList.addFooterView(footerView);
		mList.setOnScrollListener(new OnScrollListener() {

			// useless here, skip!
			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
			}

			// dumdumdum
			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {

				// what is the bottom iten that is visible
				int lastInScreen = firstVisibleItem + visibleItemCount;

				// is the bottom item visible & not loading more already ? Load
				// more !
				if ((lastInScreen == totalItemCount) && !(loadingMore)
						&& (!isEnd) && (!isFirst)) {
					loadMore();
				}
			}
		});
		UserRecommentRequest request = new UserRecommentRequest(Constant.S05, this);
		getGlobalState().getRequestQueue().addRequest(request);
	}
	private void loadMore(){
		mPageIndex++;
		UserRecommentRequest request = new UserRecommentRequest("loadmore", this);
		getGlobalState().getRequestQueue().addRequest(request);
	}
	private class UserRecommentRequest extends RequestUI{

		private Activity context;
		private FanconProgressBar mProgressDialog;
		private ArrayList<User> tmp;
		public UserRecommentRequest(Object key, Activity activity) {
			super(key, activity);
			context = activity;
			if(isFirst){
				mProgressDialog = FanconProgressBar.show(context, context.getString(R.string.common_loading));
			}
		}

		@Override
		public void execute() throws Exception {
			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
			nameValuePairs.add(new BasicNameValuePair("user_id", userID));
			nameValuePairs.add(new BasicNameValuePair("page_index", mPageIndex + ""));
			GardenJokesApiConnect api = new GardenJokesApiConnect(context);
			String result = api.execPost(getApplicationContext(), GardenJokesApiConnect.GROUP_S, "getListUserRecommended", nameValuePairs);
			Log.d("S05 result", " result: "+ result);
			JsonPaser json = new JsonPaser();
			tmp = json.getListUserRecommended(result);
			Log.d("S05 size list", "size : "+mListUser.size());
		}

		@Override
		public void executeUI(Exception ex) {
			footerView.setVisibility(View.GONE);
			
			if(mProgressDialog!=null){
				mProgressDialog.dismiss();
			}
			try {
				if (tmp.size() > 0) {
					if (mListUser.size() > 0 && !isFirst) {
						mListUser.addAll(tmp);
					} else {
						mListUser = tmp;
					}
				}
				if (tmp.size() == 0) {
					isEnd = true;
				}
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
				isEnd = true;
			}
			if(mAdapter==null){
				mAdapter = new FriendsAdapter(S05Activity.this, mListUser,userID);
				mList.setAdapter(mAdapter);
			}else {
				mAdapter.notifyDataSetChanged();
			}
			
			loadingMore = false;
			isFirst = false;
		}
		
	}
}
